<?php

namespace app\manage\services;

use app\common\loader\ModelSystem;
use app\common\loader\Services;

/**
 *  菜单管理
 */
class SystemMenuService extends BaseService
{


    protected $action = [
        'login',
        'group_menu',
        'group_permission'
    ];

    /**
     *  获取菜单列表
     * @param array $option
     * @return array
     * @throws \think\Exception
     */
    public function menuList(array $option): array
    {
        if (empty($option['field'])) {
            $option['field'] = 'id,title,pid,child,order_id,is_show';
        }
        return ModelSystem::SystemMenu()->select($option);
    }

    /**
     * 菜单详情
     * @param $option
     * @return array
     * @throws \think\Exception
     */
    public function menuInfo($option): array
    {
        if (empty($option['field'])) {
            $option['field'] = 'id,title,pid,child,order_id,front,page_action,icon';
        }
        $info = ModelSystem::SystemMenu()->find($option);
        if (empty($info)) {
            api_exception(400, '菜单不存在或已删除，请刷新后重试！');
        }
        return $info;

    }

    /**
     * 删除菜单
     * @param $id
     * @throws \think\Exception
     */
    public function menuDel($id): void
    {
        ModelSystem::SystemMenu()->del($id);
    }

    /**
     * 保存菜单
     * @param $data
     * @throws \think\Exception
     */

    public function menuSave($data): void
    {
        $data["order_id"] = !empty($data["order_id"]) ? $data["order_id"] : 200;
        // 组合前端参数
        if (!empty($data['front'])) {
            $front = $data['front'];
            $front['auth'] = $data['page_action'];
            $front['title'] = $data['title'];
            $data['front'] = json_encode($front, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
        }
        if (!empty($data['pid']) && $data['pid'] < 0) {
            $data['pid'] = 0;
        }
        ModelSystem::SystemMenu()->addEdit($data);
    }

    /**
     *  获取接口列表
     * @param array $option
     * @param int $type
     * @return array
     * @throws \think\Exception
     */
    public function interfaceList(array $option, int $type = 0): array
    {
        $list = ModelSystem::SystemApi()->select($option);
        Services::Helper()->showListTitle($list, ['system_api' => 'pid'], ['auth' => 'system.interface_auth', 'type_id' => 'system.interface_type']);
        if ($type === 2) {
            $child_count = ModelSystem::SystemApi()->where(['type_id' => ['=', 1], 'pid' => ['=', 0]])->count();
            $system_menu = config('system.system_menu');
            $list = array_merge($system_menu, $list);
            foreach ($list as &$v) {
                $v['child'] = !empty($v['child']) ? $v['child'] : 0;
                if (empty($v['id'])) {
                    $v['child'] = $child_count;
                }
            }
        }

        foreach ($list as &$v) {
            if (is_numeric($v['id']) && !empty($v['id'])) {
                $v['group_name'] = $v['pid_tag'];
            }
        }
        return $list;
    }

    /**
     * 接口详情
     * @param $option
     * @return array
     * @throws \think\Exception
     */
    public function interfaceInfo($option): array
    {
        $info = ModelSystem::SystemApi()->find($option);
        if (empty($info)) {
            api_exception(400, '接口不存在或已删除，请刷新后重试！');
        }
        return $info;
    }

    /**
     * 删除菜单
     * @param $id
     * @throws \think\Exception
     */
    public function interfaceDel($id): void
    {
        ModelSystem::SystemApi()->where(["id" => $id])->delete();
    }

    /**
     * 保存菜单
     * @param $data
     * @throws \think\Exception
     */

    public function interfaceSave($data): void
    {
        $data["sort"] = !empty($data["sort"]) ? $data["sort"] : 200;
        $id = $data['id'] ?? 0;
        unset($data['id']);
        $data['update_at'] = Dt()->format();
        if ($id > 0) {
            ModelSystem::SystemApi()->update(['id' => $id], $data);
        } else {
            $data['type_id'] = 2;
            ModelSystem::SystemApi()->insert($data);
        }
    }

    /**
     * 重新载入接口
     * @param string $module
     * @return int
     * @throws \yiqiniu\extend\exception\ApiException
     */
    public function interfaceOverload(string $module)
    {

        $jsonRules = $this->getRouteList($module);

        return ModelSystem::SystemApi()->jsonSaveToTable($jsonRules, $module);
    }

    /**
     *  获取接口列表
     * @param $group_id
     * @return array
     */
    public function userInterfaceList($group_id): array
    {
        if ($group_id > 0) {
            $where = ['role_id' => $group_id];
            return ModelSystem::SystemRolesActions()->where($where)->column(null, 'action_method');
        }
        return ModelSystem::SystemApi()->where('title', '>', 1)
            ->where('page_action', '>', 1)
            ->column(null, 'page_action');

    }
}
